<?php
require_once('./includes/headers.inc.php');
require_once('./top.php');

if(!in_array($_SESSION['user']['id'], $admin_users)) {
    $_SESSION['msg'] = $msgMgr->get('access-denied-error');
    header('Location: /');
    exit();
}

$error = FALSE;
$route_id = $_GET['id'];

if($_POST) {
    $ret = $routeMgr->update($route_id, $_POST);
    if($ret) {
        $_SESSION['msg'] = $msgMgr->get('edit-route-success');
        header('Location: /viewroute.php?id=' . $route_id);
        exit();
    } else {
        $error = $msgMgr->get('edit-route-error');
    }
}

$route = $routeMgr->get($route_id);

$difficulties = $diffMgr->getAll();
$difficulty_select = array('<select name="difficulty_id">');
foreach($difficulties as $row) {
    $difficulty_select[] =
        '<option
        value="' . $row['id'] . '" ' .
        (isset($_POST['difficulty_id']) && $_POST['difficulty_id'] == $row['id'] ? 'selected' : (!$_POST && $row['id'] == $route['difficulty_id'] ? 'selected' : '')) .
        '>' . $row['name'] . '</option>';
}
$difficulty_select[] = '</select>';

$colours = $colourMgr->getAll();
$colour_select = array('<select name="colour_id">');
foreach($colours as $row) {
    $colour_select[] =
        '<option
        value="' . $row['id'] . '" ' .
        (isset($_POST['colour_id']) && $_POST['colour_id'] == $row['id'] ? 'selected' : (!$_POST && $row['id'] == $route['colour_id'] ? 'selected' : '')) .
        '>' . $row['name'] . '</option>';
}
$colour_select[] = '</select>';

$authors = $authorMgr->getByRoute($route_id);
$author_select = array('<select name="author_id">', '<option value=""></option>');
foreach($authors as $row) {
    $author_select[] =
        '<option
        value="' . $row['id'] . '" ' .
        (isset($_POST['author_id']) && $_POST['author_id'] == $row['id'] ? 'selected' : (!$_POST && $row['id'] == $route['author_id'] ? 'selected' : '')) .
        '>' . $row['name'] . '</option>';
}
$author_select[] = '</select>';

$types = $routeMgr->getTypes();
$type_select = array('<select name="type_id">');
foreach($types as $row) {
   $type_select[] =
      '<option
      data-type="' . $row['type'] . '"
      value="'. $row['id'] . '"' .
      (isset($_POST['type_id']) && $_POST['type_id'] == $row['id'] ? 'selected' : (!$_POST && $row['id'] == $route['type_id'] ? 'selected' : '')) .
      '>' .
      ucfirst($row['name']) .
      '</option>';
}
$type_select[] = '</select>';

$replaces = array(
    'colour_select'         => implode("\n", $colour_select),
    'difficulty_select'     => implode("\n", $difficulty_select),
    'type_select'           => implode("\n", $type_select),
    'rope'                  => (isset($_POST['rope']) ? $_POST['rope'] : $route['rope']),
    'is_short'              => (isset($_POST['is_short']) ? 'checked' : ($route['is_short'] ? 'checked' : '')),
    'author_select'         => implode("\n", $author_select),
    'name'                  => (isset($_POST['name']) ? $_POST['name'] : $route['name']),
    'created'               => (isset($_POST['created']) && $_POST['created'] ? date('d-m-Y', strtotime($_POST['created'])) : (!$_POST && $route['created'] ? date('d-m-Y', strtotime($route['created'])) : '')),
    'removed'               => (isset($_POST['removed']) ? 'checked' : ($route['removed'] ? 'checked' : '')),
    'error'                 => $msgMgr->formatMsg($error),
);
$lang = array(
    'header_route_edit'         => $langMgr->get('header_route_edit'),
    'text_difficulty'           => $langMgr->get('text_difficulty'),
    'text_colour'               => $langMgr->get('text_colour'),
    'text_routetype'            => $langMgr->get('text_routetype'),
    'text_rope'                 => $langMgr->get('text_rope'),
    'text_route_short'          => $langMgr->get('text_route_short'),
    'text_author'               => $langMgr->get('text_author'),
    'text_comment'              => $langMgr->get('text_comment'),
    'text_datecreated'          => $langMgr->get('text_datecreated'),
    'text_check_routeremoved'   => $langMgr->get('text_check_routeremoved'),
    'btn_save'                  => $langMgr->get('btn_save'),
);
$templateMgr->loadContent(basename(__FILE__, '.php'), array_merge($replaces, $lang));
?>
